package com.edu.juc.completableFuture.completionStage;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

public class APITest {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
            System.out.println(Thread.currentThread().getName() + " come in");
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
//            int i = 10 / 0;
            return "hello supplyAsync";
        }).thenApply((value) -> {
            System.out.println("thenApply() come in");
            return value + "1";
        }).handle((value, e) -> {
            int i = 1 / 0;
            return value + "2";
        }).handle((value, e) -> {
            return value + "3";
        });

        System.out.println("future.get() = " + future.get()); // 阻塞方法

//        System.out.println("future.join() = " + future.join()); // 阻塞方法
//        TimeUnit.SECONDS.sleep(2);
        System.out.println(future.getNow("hello getNow")); // 现在获取值，为执行结束则返回参数值

//        System.out.println(future.complete("hello complete") + " future.get() = " + future.get());

        // null
        System.out.println(CompletableFuture.supplyAsync(() -> "hello supplyAsync").thenRun(() -> {
        }).join());
        // hello supplyAsync
        // null
        System.out.println(CompletableFuture.supplyAsync(() -> "hello supplyAsync").thenAccept(System.out::println).join());
        // hello supplyAsync
        // hello supplyAsync1
        System.out.println(CompletableFuture.supplyAsync(() -> "hello supplyAsync").thenApply(r -> {
            System.out.println(r);
            return r + "1";
        }).join());

    }
}
